package com.cc.read;


import com.cc.support.LibaryProperties;
import com.cc.support.TableStruct;
import com.cc.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @User c.c
 * @Since 2022/8/11
 * @Time 10:26
 * @Description 读取该数据库下所有表格的结构数据
 */
public class LibaryReadTable {
  String dbname = JdbcUtil.getDbname();

  public List<TableStruct> readColumn() throws SQLException {
    Connection conn = JdbcUtil.getConnection();
    String sql  = "select * from information_schema.COLUMNS WHERE TABLE_SCHEMA = "+dbname;
    conn.setAutoCommit(false);
    PreparedStatement preparedStatement = conn.prepareStatement(sql);
    ResultSet resultSet = preparedStatement.executeQuery();

    ArrayList<TableStruct> resultSets = new ArrayList<TableStruct>();
    while (resultSet.next()){
      TableStruct tableProperties = new TableStruct();
      tableProperties.setTableSchema(resultSet.getString("TABLE_SCHEMA"));
      tableProperties.setTableName(resultSet.getString("TABLE_NAME"));
      tableProperties.setColumnName(resultSet.getString("COLUMN_NAME"));
      tableProperties.setDataType(resultSet.getString("DATA_TYPE"));
      tableProperties.setColumnType(resultSet.getString("COLUMN_TYPE"));

      //设置字符长度
      if (tableProperties.getDataType().equals("varchar")||tableProperties.getDataType().equals("tinytext")||tableProperties.getDataType().equals("char")||tableProperties.getDataType().equals("varbinary")||tableProperties.getDataType().equals("text")||tableProperties.getDataType().equals("longtext")){
        tableProperties.setCharacterMaximumLength(resultSet.getString("CHARACTER_MAXIMUM_LENGTH"));
      }
      else if (tableProperties.getDataType().equals("int")||tableProperties.getDataType().equals("bigint")){
        String columnType = tableProperties.getColumnType();
        if (columnType.contains("(")||columnType.contains(")")){
          tableProperties.setCharacterMaximumLength(tableProperties.getColumnType().substring(columnType.indexOf("(")+1,columnType.indexOf(")")));
        }else {
          tableProperties.setCharacterMaximumLength(resultSet.getString("NUMERIC_SCALE"));
        }
      }else {
        String front= resultSet.getString("NUMERIC_PRECISION");
        String back = resultSet.getString("NUMERIC_SCALE");
        if (back!=null&&!back.equals("0")){
          tableProperties.setCharacterMaximumLength(front+","+back);
        }else {
          tableProperties.setCharacterMaximumLength(front);
        }
      }


      tableProperties.setColumnComment(resultSet.getString("COLUMN_COMMENT"));
      resultSets.add(tableProperties);
    }

    preparedStatement.close();
    conn.close();

    return resultSets;
  }

  /**
   * 读取该库中有哪些表
   * @author sosmcc
   * @param
   */
  public LibaryProperties readlibary()  throws SQLException{
    Connection conn = JdbcUtil.getConnection();

    //获取表格数据
    List<TableStruct> tableStructs = readColumn();

    String sql  = "SELECT * from information_schema.TABLES where TABLE_SCHEMA = "+dbname;
    conn.setAutoCommit(false);
    PreparedStatement preparedStatement = conn.prepareStatement(sql);
    ResultSet resultSet = preparedStatement.executeQuery();


    LibaryProperties libaryProperties = new LibaryProperties();
    while (resultSet.next()){
      libaryProperties.setTableName(resultSet.getString("TABLE_NAME"));
      libaryProperties.setTableComment(resultSet.getString("TABLE_COMMENT")==null?null:resultSet.getString("TABLE_COMMENT"));

      List<TableStruct> structTemp = tableStructs.stream().filter(e -> {
        return e.getTableName().equals(libaryProperties.getTableName());
      }).collect(Collectors.toList());
      libaryProperties.put(libaryProperties.getTableName(),structTemp);
    }

    preparedStatement.close();
    conn.close();
    return libaryProperties;
  }
}